package kgame5.pbe.animation {
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;

	/**
	 *
	 * @author Colbycheeze
	 * @description An animation file used by AnimationPlayer.
	 *
	 */
	public final class Animation extends EventDispatcher
	{
		/**
		 * Set this animation to loop over and over ( or not )
		 */	
		public var isLooping:Boolean;
			
		internal var _degToRotBy:Number;
		internal var _framesOfRotation:int;
		internal var _frames:Array;
		internal var _frameMap:Dictionary;
		internal var _fps:int;
		
		private var _totalFrames:int;

		/**
		 *
		 * @param a_fps The default fps of this particular animation
		 * @param a_numRotations The number of precached rotations. This cannot be changed at a later time.
		 *
		 */
		public function Animation(fps:int, numRotations:int=1)
		{
			_frames=[];
			_frameMap=new Dictionary();
			_fps=fps;

			_framesOfRotation=numRotations;
			if (_framesOfRotation < 1)
			{
				_framesOfRotation=1;
			}

			_degToRotBy=360 / _framesOfRotation;
		}
		
		/**
		 * Adds a new frame to this current animation
		 * @param a_numFrames The number of frames on the animation to play this image. ( think of frames on a movieclip )
		 * @param a_bitmapData The BitmapData to pull the image from
		 * @param a_frameLocation A rectangle definining the position of the image on the BitmapData to pull from
		 * @param a_offsetX The X offset from ( 0, 0 ). (Think registration point)
		 * @param a_offsetY The Y offset from ( 0, 0 ). (Think registration point)
		 * @param a_frameID An ID to reference this frame by. Can be left as null if not needed.
		 * @return The newly created BitmapData
		 *
		 */
		public function addFrame(numFrames:int, bitmapData:BitmapData, frameLocation:Rectangle, offsetX:Number=0, offsetY:Number=0, frameID:String=null):BitmapData
		{
			if (frameID == "")
			{
				frameID=null;
			}

			var bmpData:BitmapData=new BitmapData(frameLocation.width, frameLocation.height, true, 0x00000000);
			bmpData.copyPixels(bitmapData, frameLocation, new Point());

			_frames.push(new Frame(numFrames, bmpData, _framesOfRotation, frameID, offsetX, offsetY));
			if (frameID != null)
			{
				_frameMap[frameID]=_totalFrames;
			}
			_totalFrames++;

			return _frames[_frames.length - 1].bitmapData[0];
		}

	}
}
